package teaMainFrame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.StyledEditorKit.ForegroundAction;

import com.rORM.core.Query;
import com.rORM.core.QueryFactory;

import database.Student;
import test.test;
import util.SelectStuInfoTableModel;
import util.SelectTableModel;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.List;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JRadioButton;

public class SelectStuInfoList extends JFrame {
	
	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel model = new SelectStuInfoTableModel();
	private JLabel lblNewLabel;
	private Student student;
	private ArrayList<Student> students;
	private JTextField schoolDepartmentText;
	private JTextField textField_1;
	private JTextField levelText;
	private JTextField classText;
	private JTextField nameText;
	private JTextField idText;
	
	private int flag;//判断是使用哪种渠道查找学生信息
	private int id;
	private String name;
	private int entranceTime;
	private int classNum;
	private String schoolDepartment;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectStuInfoList frame = new SelectStuInfoList();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectStuInfoList(){
		
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 855, 591);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 88, 789, 355);
		contentPane.add(scrollPane);
		
		table = new JTable(model);
		scrollPane.setViewportView(table);
		
		lblNewLabel = new JLabel("查询学生信息");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 23));
		lblNewLabel.setBounds(319, 16, 175, 48);
		contentPane.add(lblNewLabel);
		
		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		button.setBounds(732, 534, 117, 29);
		contentPane.add(button);
		
		JLabel label = new JLabel("请输入学生班级");
		label.setBounds(37, 472, 117, 16);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("系");
		label_1.setBounds(202, 477, 28, 16);
		contentPane.add(label_1);
		
		schoolDepartmentText = new JTextField();
		schoolDepartmentText.setColumns(10);
		schoolDepartmentText.setBounds(154, 472, 46, 26);
		contentPane.add(schoolDepartmentText);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(292, 472, -15, 26);
		contentPane.add(textField_1);
		
		JLabel label_2 = new JLabel("班");
		label_2.setBounds(303, 477, 28, 16);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("级");
		label_3.setBounds(263, 477, 28, 16);
		contentPane.add(label_3);
		
		levelText = new JTextField();
		levelText.setColumns(10);
		levelText.setBounds(212, 472, 52, 26);
		contentPane.add(levelText);
		
		classText = new JTextField();
		classText.setColumns(10);
		classText.setBounds(272, 472, 34, 26);
		contentPane.add(classText);
		
		JLabel label_4 = new JLabel("请输入学生姓名");
		label_4.setBounds(35, 508, 117, 16);
		contentPane.add(label_4);
		
		nameText = new JTextField();
		nameText.setColumns(10);
		nameText.setBounds(164, 503, 130, 26);
		contentPane.add(nameText);
		
		JLabel label_5 = new JLabel("请输入学生学号");
		label_5.setBounds(37, 547, 117, 16);
		contentPane.add(label_5);
		
		idText = new JTextField();
		idText.setColumns(10);
		idText.setBounds(166, 534, 130, 26);
		contentPane.add(idText);
		
		JRadioButton radioButton = new JRadioButton("通过班级查询");
		radioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				flag = 3;
			}
		});
		radioButton.setBounds(343, 472, 141, 23);
		contentPane.add(radioButton);
		
		JRadioButton radioButton_1 = new JRadioButton("通过姓名查询");
		radioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				flag = 2;
			}
		});
		radioButton_1.setBounds(343, 508, 141, 23);
		contentPane.add(radioButton_1);
		
		JRadioButton radioButton_2 = new JRadioButton("通过学号查询");
		radioButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				flag = 1;
			}
		});
		radioButton_2.setBounds(343, 543, 141, 23);
		contentPane.add(radioButton_2);
		
		JButton button_1 = new JButton("查询");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				model.getDataVector().clear();
				ArrayList<Student> students = null;
				if(flag == 1){
					id = Integer.parseInt(idText.getText());
						students = getStudents(id);
				}else if(flag == 2){
					name = nameText.getText();
					students = getStudents(name);
				}else if(flag == 3){
					entranceTime = Integer.parseInt(levelText.getText());
					classNum = Integer.parseInt(classText.getText());
					schoolDepartment = schoolDepartmentText.getText();
					students = getStudents(entranceTime, classNum,schoolDepartment);
				}
				
				try{
					for(Student s : students){
				        model.addRow(getStuInfo(s));			            
					}	
				}catch (NullPointerException e1) {
					JOptionPane.showMessageDialog(null,"无学生","信息提示框",JOptionPane.INFORMATION_MESSAGE);
				}
				
			}
		});
		button_1.setBounds(566, 534, 117, 29);
		contentPane.add(button_1);
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(radioButton_2);
		bg.add(radioButton_1);
		bg.add(radioButton);
		
		
	}
	
	

	
	public Object[] getStuInfo(Student s){
		return new Object[]{
				s.getId(),s.getName(),
				s.getSchool_department(),s.getSex(),
				s.getBirth(),s.getPersonalID(),
				s.getCollege_time(),s.getPolitics(),
				s.getAdress(),s.getNative_place(),
				s.getNation()
		};
	}
	
	public ArrayList<Student> getStudents(int id){
		ArrayList<Student> students;
		Query q = QueryFactory.createQuery();
		students = (ArrayList<Student>) q.queryRows("select * from student where id = ?", Student.class, new Object[]{id});
		return students;
	}
	
	public ArrayList<Student> getStudents(String name){
		ArrayList<Student> students;
		Query q = QueryFactory.createQuery();
		students = (ArrayList<Student>) q.queryRows("select * from student where name = ?", Student.class, new Object[]{name});
		return students;
	}
	
	
	public ArrayList<Student> getStudents(int entranceTime,int classNum,String schoolDepartment){
		ArrayList<Student> students;
		Query q = QueryFactory.createQuery();
		students = (ArrayList<Student>) q.queryRows("select * from student where college_time = ? and classNum = ? and school_department = ?;", Student.class, new Object[]{entranceTime,classNum,schoolDepartment});
		return students;
	}
}
